﻿Imports System.Web.UI
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.BusinessLayer.Views
Imports SISSAT.WebDevelopment.Views

Namespace [Shared].UserControls
    Public Class EmployeeDetailControl
        Inherits UserControl
        Implements IEmployeeDetailControlView

#Region "Fields"

        Private _controlMode As PageModes

#End Region

#Region "Properties"

        Public Property ErrorMessage As String
        Public Property UseControlModeFromUserInterface As Boolean

        Public Property ControlMode As PageModes
            Get
                Return _controlMode
            End Get
            Set(value As PageModes)
                If UseControlModeFromUserInterface Then
                    Exit Property
                End If

                _controlMode = value
                IsReadOnly = Not (_controlMode = PageModes.EditMode)
                CssClass = Convert.ToString(IIf(IsReadOnly, "textbox readonly", "textbox req"))

                InitializeComponents()
            End Set
        End Property

        Public Property ActiveViewIndex As Integer
            Get
                Return EmployeeDetailControlViews.ActiveViewIndex
            End Get
            Set(value As Integer)
                EmployeeDetailControlViews.ActiveViewIndex = value
                ButtonViews.ActiveViewIndex = value
                FullName = String.Format("{0} {1}", FirstName, LastName)
            End Set
        End Property

        Public WriteOnly Property FullName As String
            Set(value As String)
                FullNameTextBox.Text = value
            End Set
        End Property

        Private Property Presenter As EmployeeDetailControlPresenter
        Private Property CssClass As String
        Private Property IsReadOnly As Boolean

#End Region

#Region "IEmployeeDetailControlView Members"

        Public Property ContactNumber As String Implements IEmployeeDetailControlView.ContactNumber
            Get
                Return ContactNumberTextBox.Text
            End Get
            Set(value As String)
                ContactNumberTextBox.Text = value
            End Set
        End Property

        Public Property Course As String Implements IEmployeeDetailControlView.Course
            Get
                Return CourseControl.SelectedControlValue
            End Get
            Set(value As String)
                CourseControl.SelectedControlValue = value
            End Set
        End Property

        Public ReadOnly Property CurrentSchoolYear As Integer Implements IEmployeeDetailControlView.CurrentSchoolYear
            Get
                Return SISSATPageBase.SchoolYear
            End Get
        End Property

        Public ReadOnly Property CurrentSemester As Integer Implements IEmployeeDetailControlView.CurrentSemester
            Get
                Return Convert.ToInt32(SISSATPageBase.Semester)
            End Get
        End Property

        Public ReadOnly Property CurrentUserId As Integer Implements IEmployeeDetailControlView.CurrentUserId
            Get
                Return SISSATPageBase.User.Employee.EmployeeId
            End Get
        End Property

        Public Property EmailAddress As String Implements IEmployeeDetailControlView.EmailAddress
            Get
                Return EmailAddressTextBox.Text
            End Get
            Set(value As String)
                EmailAddressTextBox.Text = value
            End Set
        End Property

        Public Property EmployeeCode As String Implements IEmployeeDetailControlView.EmployeeCode
            Get
                Dim code As String = HttpContext.Current.Request.QueryString("code")
                If String.IsNullOrEmpty(code) Then
                    code = SISSATPageBase.User.Employee.EmployeeCode
                    ControlMode = PageModes.EditPartiallyMode
                ElseIf String.Compare(code, "new", True) = 0 Then
                    code = String.Empty
                    ControlMode = SISSATPageBase.AppropriatePageModeForUser()
                Else
                    ControlMode = SISSATPageBase.AppropriatePageModeForUser()
                End If

                Return code
            End Get
            Set(value As String)
                IdTextBox.Text = value
            End Set
        End Property

        Public ReadOnly Property EmployeeTypeId As Integer Implements IEmployeeDetailControlView.EmployeeTypeId
            Get
                Dim id As Integer = 0
                If HttpContext.Current.Request.QueryString("tid") IsNot Nothing Then
                    Integer.TryParse(HttpContext.Current.Request.QueryString("tid"), id)
                End If

                Return id
            End Get
        End Property

        Public Property FirstName As String Implements IEmployeeDetailControlView.FirstName
            Get
                Return FirstNameTextBox.Text
            End Get
            Set(value As String)
                FirstNameTextBox.Text = value
            End Set
        End Property

        Public Property HomeAddress As String Implements IEmployeeDetailControlView.HomeAddress
            Get
                Return String.Format("{0} {1}", HomeAddressTextBox.Text.Trim(), HomeAddressExtensionTextBox.Text.Trim())
            End Get
            Set(value As String)
                HomeAddressTextBox.Text = value
            End Set
        End Property

        Public Property LastName As String Implements IEmployeeDetailControlView.LastName
            Get
                Return LastNameTextBox.Text
            End Get
            Set(value As String)
                LastNameTextBox.Text = value
            End Set
        End Property

        Public Property MiddleName As String Implements IEmployeeDetailControlView.MiddleName
            Get
                Return MiddleNameTextBox.Text
            End Get
            Set(value As String)
                MiddleNameTextBox.Text = value
            End Set
        End Property

        Public Property Year As String Implements IEmployeeDetailControlView.Year
            Get
                Return YearTextBox.Text
            End Get
            Set(value As String)
                YearTextBox.Text = value
            End Set
        End Property

        Public Event OnClear() Implements IEmployeeDetailControlView.OnClear
        Public Event OnInitialization() Implements IEmployeeDetailControlView.OnInitialization
        Public Event OnSubmit() Implements IEmployeeDetailControlView.OnSubmit

#End Region

#Region "Methods"

        Private Sub InitializeComponents()
            ActiveViewIndex = Convert.ToInt32(IIf(IsReadOnly, 0, 1))
            ContactNumberTextBox.ReadOnly = IsReadOnly
            ContactNumberTextBox.CssClass = CssClass
            CourseControl.IsReadOnly = IsReadOnly
            EmailAddressTextBox.ReadOnly = IsReadOnly
            EmailAddressTextBox.CssClass = CssClass
            FirstNameTextBox.ReadOnly = IsReadOnly
            FirstNameTextBox.CssClass = CssClass
            HomeAddressTextBox.ReadOnly = IsReadOnly
            HomeAddressTextBox.CssClass = CssClass
            HomeAddressExtensionTextBox.ReadOnly = IsReadOnly
            LastNameTextBox.ReadOnly = IsReadOnly
            LastNameTextBox.CssClass = CssClass
            MiddleNameTextBox.ReadOnly = IsReadOnly
            MiddleNameTextBox.CssClass = CssClass
            YearTextBox.ReadOnly = IsReadOnly
            YearTextBox.CssClass = CssClass

            Select Case ControlMode
                Case PageModes.ViewPartiallyMode
                    ActiveViewIndex = 0
                Case PageModes.EditMode
                    MiddleNameTextBox.CssClass = "textbox"
                Case PageModes.EditPartiallyMode
                    ActiveViewIndex = 1
                    ContactNumberTextBox.ReadOnly = Not IsReadOnly
                    ContactNumberTextBox.CssClass = "textbox req"
                    EmailAddressTextBox.ReadOnly = Not IsReadOnly
                    EmailAddressTextBox.CssClass = "textbox req"
                    HomeAddressTextBox.ReadOnly = Not IsReadOnly
                    HomeAddressTextBox.CssClass = "textbox req"
                    HomeAddressExtensionTextBox.ReadOnly = Not IsReadOnly
                    MiddleNameTextBox.ReadOnly = IsReadOnly
                    MiddleNameTextBox.CssClass = "textbox readonly"
            End Select
        End Sub

#End Region

#Region "Events"

        Private Sub PageInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
            ContactNumberRegExValidator.ValidationExpression = ConfigurationManager.AppSettings("ContactNumberRegEx")
            EmailAddressRegExValidator.ValidationExpression = ConfigurationManager.AppSettings("EmailRegEx")
            YearRegExValidator.ValidationExpression = ConfigurationManager.AppSettings("YearRegEx")

            Presenter = New EmployeeDetailControlPresenter(Me)
            RaiseEvent OnInitialization()

            InitializeComponents()
        End Sub

        Private Sub ClearButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles ClearButton.Click
            RaiseEvent OnClear()

            Me.HomeAddressExtensionTextBox.Text = String.Empty
            Me.ErrorMessage = "Reset of data is successful."
        End Sub

        Private Sub SubmitButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles SubmitButton.Click
            RaiseEvent OnSubmit()

            Me.ErrorMessage = "Save is successful."
        End Sub

#End Region

    End Class
End Namespace
